package br.com.enovar.controller;

import javax.inject.Inject;
import javax.servlet.ServletException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import br.com.enovar.classe.PriceIncrease;
import br.com.enovar.interfaces.ProductManager;

@Controller
public class PriceIncreaseController {
	/** Logger for this class and subclasses */
	protected final Log logger = LogFactory.getLog(getClass());

	private ProductManager productManager;

	@Inject
	public PriceIncreaseController(ProductManager productManager) {
		this.productManager = productManager;
	}

	@RequestMapping(value="/priceincrease" , method=RequestMethod.POST)
	public ModelAndView save(PriceIncrease command) throws ServletException {

		int increase = command.getPercentage();
		logger.info("Increasing prices by " + increase + "%.");

		productManager.increasePrice(increase);

		logger.info("returning from PriceIncreaseForm view to hello");

		return new ModelAndView("redirect:/app/hello");
	}

	@RequestMapping(value = "/priceincrease/new", method = RequestMethod.GET)
	protected ModelAndView newPriceIncrease() throws ServletException {
		PriceIncrease priceIncrease = new PriceIncrease();
		priceIncrease.setPercentage(20);
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("priceincrease");
		modelAndView.addObject("command",priceIncrease);
		return modelAndView;
	}

}
